import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Product } from 'src/types/product.interface';

@Injectable()
export class ProductService {
  constructor(@InjectModel('Product') private productModel: Model<Product>) {}

  async find(query): Promise<Product[]> {
    return this.productModel.find(query).exec();
  }
  async create(
    createProductDTO: Product | Product[],
  ): Promise<Product | Product[]> {
    if (Array.isArray(createProductDTO)) {
      return this.productModel.insertMany(createProductDTO);
    } else {
      return new this.productModel(createProductDTO).save();
    }
  }
  async findPremiumProducts(): Promise<Product[]> {
    return this.productModel
      .find({ isPremium: true, isActive: true })
      .sort({ createdAt: -1 })
      .limit(6)
      .exec();
  }
}
